﻿using EasyLanguage.Data.Contracts.Repository;
using EasyLanguage.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyLanguage.DataAccess.Repository
{
    public class UserRepository : EFRepository<User>, IUserRepository
    {
        private readonly ICountryRepository _countryRepository;
        private readonly IAccountRepository _accountRepository;

        public UserRepository(DbContext dbContext, ICountryRepository countryRepository, IAccountRepository accountRepository)
            : base(dbContext)
        {
            _countryRepository = countryRepository;
            _accountRepository = accountRepository;
        }

        public User GetByAccountId(int accountId)
        {
            return DbSet.FirstOrDefault(x => x.AccountId == accountId);
        }

        public void SaveUser(User user, Account userAccount, Country country)
        {
            // First save user account
            _accountRepository.SaveAccount(userAccount);
            user.AccountId = userAccount.AccountId;
            // Set Country Id
            user.CountryId = country.CountryId;

            Add(user);
        }

        public void DeleteUser(User user)
        {
            Delete(user);
        }
    }
}
